﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ExampleExam
{
    class Exam
    {
        public static void TelRegex()
        {
            string telPattern = "[(]{1}[0-9]{3}[)]{1}[ ]{1}[0-9]{3}[ ]{1}[0-9]{3}[-]{1}[0-9]{3}";
            Regex reg = new Regex(telPattern);
            string textFile = "example.txt";
            StreamReader reader = new StreamReader(textFile);
            using (reader)
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    MatchCollection mathes = reg.Matches(line);
                    foreach (var item in mathes)
                    {
                        Console.WriteLine(item.ToString());
                    }
                }
            }
        }

        public static void SplitBinFile()
        {
            const int SIZE = 5120;
            string inputFile = "input.bin";
            FileStream input = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
            using (input)
            {
                string outputFileOne = "out1.bin";
                FileStream outputOne = new FileStream(outputFileOne, FileMode.Create, FileAccess.Write);
                using (outputOne)
                {
                    string outputFileTwo = "out2.bin";
                    FileStream outputTwo = new FileStream(outputFileTwo, FileMode.Create, FileAccess.Write);
                    using (outputTwo)
                    {
                        int currFile = 1;
                        byte[] buffy = new byte[SIZE];
                        while (true)
                        {
                            int bytesRead = input.Read(buffy, 0, SIZE);
                            if (bytesRead == 0)
                            {
                                break;
                            }
                            if (currFile == 1)
                            {
                                outputOne.Write(buffy, 0, bytesRead);
                                currFile = 2;
                            }
                            else if (currFile == 2)
                            {
                                outputTwo.Write(buffy, 0, bytesRead);
                                currFile = 1;
                            }
                        }
                    }
                }
            }
        }
        public static void RecreateBinFile()
        {
            string inputFileOne = "out1.bin";
                FileStream inputOne = new FileStream(inputFileOne, FileMode.Open, FileAccess.Read);
                using (inputOne)
                {
                    string inputFileTwo = "out2.bin";
                    FileStream inputTwo = new FileStream(inputFileTwo, FileMode.Open, FileAccess.Read);
                    using (inputTwo)
                    {
                        string outputFile = "newPic.bin";
                        FileStream output = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
                        using (output)
                        {
                            bool[] finished = new bool[3];
                            const int SIZE = 5120;
                            int currFile = 1;
                            byte[] buffy = new byte[SIZE];
                            while (true)
                            {
                                int byteRead = 0;
                                if (currFile == 1)
                                {
                                    byteRead = inputOne.Read(buffy, 0, SIZE);
                                    currFile = 2;
                                }
                                else if (currFile == 2)
                                {
                                    byteRead = inputTwo.Read(buffy, 0, SIZE);
                                    currFile = 1;
                                }
                                if (byteRead == 0)
                                {
                                    if (finished[1] && finished[2])
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        if (currFile == 2)
                                        {
                                            finished[1] = true;
                                        }
                                        else
                                        {
                                            finished[2] = true;
                                        }
                                    }
                                }
                                output.Write(buffy, 0, byteRead);
                            }
                        }
                    }
                }
        }

        static void Main()
        {
            SplitBinFile();
            RecreateBinFile();
        }
    }
}
